package com.iamback.dy.examine.service.impl;

import com.iamback.dy.entity.*;
import com.iamback.dy.examine.common.Page;
import com.iamback.dy.examine.common.utils.StringUtils;
import com.iamback.dy.examine.service.*;
import com.iamback.dy.examine.vo.ContentComplainVO;
import com.iamback.dy.examine.vo.DyUserInfoVO;
import com.iamback.dy.examine.vo.req.ImpaechReq;
import com.iamback.dy.examine.vo.req.ImpaechUserReq;
import com.iamback.dy.mapper.*;

import freemarker.template.utility.NumberUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.NumberUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by heanyinqin on 2018/8/4.
 */

@Service
public class DyImpeachServiceImpl implements IDyImpeachService {

    @Autowired
    private DyImpeachMapper dyImpeachMapper;

    @Autowired
    private ContentUnreviewedMapper contentUnreviedMapper;

    @Autowired
    private DyContentMapper dyContentMapper;

    @Autowired
    private ContentExuserMapper contentExuserMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private DyUserInfoMapper dyUserInfoMapper;

    @Autowired
    private DyUserMapper dyUserMapper;

    @Override
    public Page<ContentComplainVO> getPageByType(ImpaechReq req) {
        int type = req.getImpaechType();
        int pageNum = req.getPageNumber();
        int pageSize = req.getPageSize();
        int start = pageNum ==0?0:(pageNum-1)*pageSize;
        List<Long> contentIds = new ArrayList<>();
        List<String> dyIds = new ArrayList<>();

        if (!StringUtils.isBlank(req.getContentText())){
            List<DyContent> contents =  dyContentMapper.getListByText(req.getContentText());
            if ((!CollectionUtils.isEmpty(contents)) && contents.size()>0){
                for (int i = 0; i < contents.size(); i++) {
                    DyContent content = contents.get(i);
                    if (content !=null){
                        contentIds.add(content.getDyContentId());
                    }
                }
            }
        }

        if (!StringUtils.isBlank(req.getNeckName())){
            List<String> dyIdList = new ArrayList<>();
            List<DyUserInfo> userInfos = dyUserInfoMapper.getListByNeckName(req.getNeckName());
            if ((!CollectionUtils.isEmpty(userInfos)) && userInfos.size()>0){
                for (int i = 0; i < userInfos.size(); i++) {
                    DyUserInfo info = userInfos.get(i);
                    if (info !=null) {
                        dyIdList.add(info.getDyId());
                    }
                }
            }
            if (!CollectionUtils.isEmpty(dyIdList) && dyIdList.size() > 0 ){
                Map<String,Object> map = new HashMap<>();
                map.put("contentIds",dyIdList);
                List<DyContent> contents =  dyContentMapper.getListByDyIds(map);
                if ((!CollectionUtils.isEmpty(contents)) && contents.size()>0){
                    for (int i = 0; i < contents.size(); i++) {
                        DyContent content = contents.get(i);
                        if (content !=null){
                            contentIds.add(content.getDyContentId());
                        }
                    }
                }
            }
        }

        Map<String, Object> map=new HashMap<String, Object>();
        map.put("type", 1);
        map.put("pageNum", start);
        map.put("pageSize", pageSize);
        map.put("contentIds",contentIds);
        map.put("dyIds",dyIds);
        List<DyImpeach> list = dyImpeachMapper.getPageByType(map);
        Page<ContentComplainVO> page = new Page<>();
        int total = dyImpeachMapper.getCounts(map);
        List<ContentComplainVO> contentList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)){
            for (int i = 0; i < list.size(); i++) {
                DyImpeach impeach = list.get(i);
                if (impeach !=null && impeach.getContentId() >0){
                    ContentExuser query = new ContentExuser();
                    ContentComplainVO vo = new ContentComplainVO();
                    vo.setImpeachCount(impeach.getImpeachCount());
                    query.setDyContentId(Long.valueOf(impeach.getContentId()));
                    DyContent content = dyContentMapper.getById(impeach.getContentId());
//                    System.out.println("content:"+content);
                    ContentExuser contentex = contentExuserMapper.findByDyId(query);
                    if (content !=null){
                        DyUserInfo userInfo = dyUserInfoMapper.findDyUserInfoById(content.getPublisherDyId());
                        vo.setDyContentId(content.getDyContentId());
                        vo.setContentText(content.getContentText());
                        vo.setContentType(content.getContentType());
                        vo.setContentUrl(content.getContentUrl());
                        vo.setImpeachCount(impeach.getImpeachCount());
                        vo.setPixelHeight(content.getPixelHeight());
                        vo.setPixelWidth(content.getPixelWidth());
                        vo.setPublisherDyId(userInfo.getDyId());
                        vo.setPublisherDyName(userInfo.getNickName());
                        vo.setUploadDate(content.getUploadDate());
                        if (contentex !=null){
                            if (contentex.getExuserId() != null && contentex.getExuserId() > 0){
                                User exuser = userMapper.getUserById(Integer.valueOf(contentex.getExuserId().toString()));
                                vo.setExuserName(exuser.getUserName());
                            }
                            vo.setExDesc(contentex.getExDesc());
                            vo.setExdyId(contentex.getExdyId());
                            vo.setExResult(contentex.getExResult());
                            vo.setExTime(contentex.getExTime());
                            vo.setExuserId(contentex.getExuserId());
                        }
                        contentList.add(vo);
                    }
                }
            }
        }
        page.setContent(contentList);
        page.setTotalElements(total);
        return page;
    }

    @Override
    public Page<DyUserInfoVO> getImpaechUserPage(ImpaechUserReq req) {
        Page<DyUserInfoVO> page = new Page<>();
        List<DyUserInfoVO> userList = new ArrayList<>();
        int type = req.getImpaechType();
        int pageNum = req.getPageNumber();
        int pageSize = req.getPageSize();
        List<Long> dyIds = new ArrayList<>();
        int start = pageNum ==0?0:(pageNum-1)*pageSize;

        if (!StringUtils.isBlank(req.getDyId()) && !"0".equals(req.getDyId())){
            dyIds.add(Long.valueOf(req.getDyId()));
        }
        if (!StringUtils.isBlank(req.getNeckName())){
            List<String> dyIdList = new ArrayList<>();
            List<DyUserInfo> userInfos = dyUserInfoMapper.getListByNeckName(req.getNeckName());
            if ((!CollectionUtils.isEmpty(userInfos)) && userInfos.size()>0){
                for (int i = 0; i < userInfos.size(); i++) {
                    DyUserInfo info = userInfos.get(i);
                    dyIds.add(Long.valueOf(info.getDyId()));
                }
            }
        }

        Map<String, Object> map=new HashMap<>();
        map.put("type", 0);
        map.put("pageNum", start);
        map.put("pageSize", pageSize);
        map.put("dyIds",dyIds);
        List<DyImpeach> impeachList = dyImpeachMapper.getPageByType(map);
        int total = dyImpeachMapper.getCounts(map);
        if (!CollectionUtils.isEmpty(impeachList)) {
            for (int i = 0; i < impeachList.size(); i++) {
                DyImpeach impeach = impeachList.get(i);
                if (impeach !=null && (!StringUtils.isBlank(impeach.getDyId()) && !"0".equals(impeach.getDyId())) ){
                    DyUserInfoVO vo = new DyUserInfoVO();
                    vo.setImpeachCount(impeach.getImpeachCount());
                    DyUserInfo info = dyUserInfoMapper.findDyUserInfoById(impeach.getDyId());
                    DyUser user = dyUserMapper.findDyUserByDyId(impeach.getDyId());
                    if (info !=null){
                        vo.setDyId(info.getDyId());
                        vo.setHeadPortraitUrl(info.getHeadPortraitUrl());
                        vo.setNickName(info.getNickName());
                        vo.setSignature(info.getSignature());
                    }
                    if (user != null){
                        vo.setCreateData(user.getCreateData());
                        vo.setMobilePhone(user.getMobilePhone());
                        vo.setUserStatus(user.getUserStatus());
                        vo.setStatusDeadline(user.getStatusDeadline());
                    }
                    userList.add(vo);
                }
            }
        }
        page.setContent(userList);
        page.setTotalElements(total);

        return page;
    }

    @Override
    public void deleteContentById(Long contentId) {
        Map<String,Object> map = new HashMap<>();
        map.put("isApproved",3);
        map.put("dyContentId",contentId);
        contentUnreviedMapper.updateContentUnrevieweByStatus(map);
        contentUnreviedMapper.deleteDyContent(contentId);
        int ret = dyImpeachMapper.updateHandle(contentId);
    }
}
